/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor;

import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.JTextComponent;

import org.opu.vcforms.editor.actions.EventBusEditAction;

/**
 * 
 * @author yura
 */
public class TabbedEditViewFactory extends EditViewFactory {

    private TabbedEditView view = new TabbedEditView();
    private JFrame fr;

    @Override
    protected EditView getView(String text, String title, File file) {
        TextView tv = mkTextView(text, file);
        JTextComponent jtv;

        if (tv instanceof JTextComponent) {
            jtv = (JTextComponent) tv;
        } else {
            throw new NullPointerException("Text view must be a component");
        }

        if (fr == null || !fr.isVisible()) {
            view = new TabbedEditView();
            fr = mkFrame(new EventBusEditAction(view));
            fr.getContentPane().add(view);
            view.setFrame(fr);
            fr.setVisible(true);
        }

        String tip = file != null? file.getName(): null;
        view.addTab(title, null, new JScrollPane(jtv), tip);

        return view;
    }
}
